#!/bin/bash
# Script que permite configurar los servidores WEB, FTP y TELNET.
#
#


########### -------- NO TOCAR DESDE ACA ---------#####################

# Archivo de constantes
. ./const.sh
# Archivo de funciones
. ./utils.sh


function commonRouter(){

	log "INICIANDO CONFIGURACION DE ROUTER $1  "
	log "--------------------------------------"

	log "Limpiando interfaces del servidor ..."
	limpiarInterfaces

	IF=`$IFCONFIG | egrep "eth" | sed 's/^\([A-Za-z0-9:]*\).*/\1/'`
	IF2="$IF:0"
	IF3="$IF:1"
	
	log "Activando forwardeo de paquetes ..."
	forwardeo on
	
}

function h2(){

	commonRouter "H2"
	
	log "Configurando IP ..."
	debug on
	$IFCONFIG $IF $H2_MARMOSA netmask $MASK26
	$IFCONFIG $IF2 $H2_MURCIELAGO netmask $MASK30
	debug off

	log "Configurando tablas de ruteo ..."
	debug on
	#$ROUTE_MARMOSA $IF
	#$ROUTE_MURCIELAGO $IF2
	$ROUTE_MULITA $IF gw $H4_MARMOSA
	$ROUTE add default gw $H13_MURCIELAGO
	debug off
	
	log "Aplicando configurando DNS Slave - Resto"	
	$DNS_SCRIPT slave
	checkStatusAbort

	log "Aplicando configuracion de RESOLV.CONF - Resto ..."
	$DNS_SCRIPT resolv resto

	
}

function h4(){
	commonRouter "H4"

	log "Configurando IP ..."
	debug on
	$IFCONFIG $IF $H4_MULITA netmask $MASK24
	$IFCONFIG $IF2 $H4_MARMOSA netmask $MASK26
	debug off

	log "Configurando tablas de ruteo ..."
	debug on
	# $ROUTE_MULITA $IF
	# $ROUTE_MARMOSA $IF2
	$ROUTE add default gw $H2_MARMOSA
	debug off

	log "Aplicando configuracion de RESOLV.CONF - Resto ..."
	$DNS_SCRIPT resolv resto

}

function h13(){
	commonRouter "H13"

	log "Configurando IP ..."
	debug on
	$IFCONFIG $IF $H13_MURCIELAGO netmask $MASK30
	$IFCONFIG $IF2 $H13_MONO netmask $MASK25
	$IFCONFIG $IF3 $H13_HUEMUL netmask $MASK28
	debug off

	log "Configurando tablas de ruteo ..."
	debug on
	# $ROUTE_MURCIELAGO $IF 
	# $ROUTE_MONO $IF2
	# $ROUTE_HUEMUL $IF3
	$ROUTE_MARMOSA $IF gw $H2_MURCIELAGO
	$ROUTE_MULITA $IF gw $H2_MURCIELAGO
	$ROUTE add default gw $H15_HUEMUL
	debug off

	log "Aplicando configurando DNS ROOT"	
	$DNS_SCRIPT master
	checkStatusAbort

	log "Aplicando configuracion de RESOLV.CONF - Resto ..."
	$DNS_SCRIPT resolv resto
	
}


function h15(){
	commonRouter "H15"

	log "Configurando IP ..."
	debug on
	$IFCONFIG $IF $H15_HUEMUL netmask $MASK28
	$IFCONFIG $IF2 $H15_MOLOSO netmask $MASK30
	debug off

	log "Configurando tablas de ruteo ..."
	debug on
	# $ROUTE_HUEMUL $IF
	# $ROUTE_MOLOSO $IF2
	$ROUTE_YAGUARETE $IF2 gw $H34_MOLOSO
	$ROUTE_QUIRQUINCHO $IF2 gw $H34_MOLOSO
	$ROUTE_GATO $IF2 gw $H34_MOLOSO
	$ROUTE add default gw $H13_HUEMUL
	debug off

	log "Aplicando configuracion de RESOLV.CONF - Resto ..."
	$DNS_SCRIPT resolv resto

}

function h31(){
	commonRouter "H31"

	log "Configurando IP ..."
	debug on
	$IFCONFIG $IF $H31_YAGUARETE netmask $MASK27
	$IFCONFIG $IF2 $H31_QUIRQUINCHO netmask $MASK27
	$IFCONFIG $IF3 $H31_GATO netmask $MASK24
	debug off

	log "Configurando tablas de ruteo ..."
	debug on
	# $ROUTE_YAGUARETE $IF
	# $ROUTE_QUIRQUINCHO $IF2
	# $ROUTE_GATO $IF3
	$ROUTE add default gw $H34_QUIRQUINCHO
	debug off

	log "Aplicando configurando DNS Slave - Concaran"	
	$DNS_SCRIPT slave
	checkStatusAbort

	log "Aplicando configuracion de RESOLV.CONF - Concaran ..."
	$DNS_SCRIPT resolv concaran


}

function h34(){
	commonRouter "H34"

	log "Configurando IP ..."
	debug on
	$IFCONFIG $IF $H34_QUIRQUINCHO netmask $MASK27
	$IFCONFIG $IF2 $H34_MOLOSO netmask $MASK30
	debug off

	log "Configurando tablas de ruteo ..."
	debug on
	# $ROUTE_QUIRQUINCHO $IF
	# $ROUTE_MOLOSO $IF2
	$ROUTE_YAGUARETE $IF gw $H31_QUIRQUINCHO
	$ROUTE_GATO $IF gw $H31_QUIRQUINCHO
	$ROUTE add default gw $H15_MOLOSO
	debug off

	log "Aplicando configuracion de RESOLV.CONF - Concaran ..."
	$DNS_SCRIPT resolv concaran

}



function ayuda(){

	echo "Error en los parametros"
	echo "-----------------------"
	echo "Los parametros validos de este script son:"
	echo ""
	echo " h2 : configura el router H2"
	echo " h4 : configura el router H4"
	echo " h13 : configura el router H13"
	echo " h15 : configura el router H15"
	echo " h31 : configura el router H31"
	echo " h34 : configura el router H34"

}


######## FIN FUNCIONES - inicio ejecucion #################

inicio

case $1 in
h2) h2;;
h4) h4;;
h13) h13;;
h15) h15;;
h31) h31;;
h34) h34;;
*) ayuda
esac 
